package sm.upgrade;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.dam.api.DBDictService;
import sm.dbdict.DBDictUtil;
import sm.dbdict.def.DBDict;
import sm.dbdict.def.Field;
import sm.dbdict.def.Key;
import sm.def.KeyType;
import sm.def.Type;
import sm.upgrade.api.UpgradePhase;
import sm.util.RADFunction;
import sm.util.SMMap;

public class DataPolicyUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	public void upgrade2() {
		logger.info("Updating Data Policy records...");

		SMMap datadictMap = (SMMap) template.get(DBDictService.DBDICT,
				"datadict");
		DBDict datadict = (DBDict) datadictMap.get(DBDictService.DESCRIPTOR);

		Field extattrnameField = DBDictUtil.getField("extattrname",
				datadict.getField());
		boolean isExtattrnameArray = true;
		if (extattrnameField != null
				&& extattrnameField.getType() != Type.ARRAY_TYPE) {
			isExtattrnameArray = false;
		}
		boolean isGloballistArray = true;
		Field globallistField = DBDictUtil.getField("globallist",
				datadict.getField());
		if (globallistField != null
				&& globallistField.getType() != Type.ARRAY_TYPE) {
			isGloballistArray = false;
		}

		List<Object> dbdicts = template.find("from dbdict");
		int total = dbdicts.size();
		for (Object dbdictObj : dbdicts) {
			SMMap dbdictMap = (SMMap) dbdictObj;
			String name = (String) dbdictMap.get(DBDictService.NAME);
			DBDict dbdict = (DBDict) dbdictMap.get(DBDictService.DESCRIPTOR);
			SMMap file = (SMMap) template.get("datadict", name);
			if (file == null) {
				file = new SMMap();
			}
			String sql = "name =" + name;
			String sqlBaseName = name;
			int sqlBaseNameMax = 11;
			if (sqlBaseName.length() > sqlBaseNameMax) {
				sqlBaseName = sqlBaseName.substring(0, sqlBaseNameMax);
			}
			int sqlBaseNameCount = 0;

			// us.gen.datadict
			if (!isExtattrnameArray) {
				file.put("extattrname", Collections.EMPTY_LIST);
			}
			if (!isGloballistArray) {
				file.put("globallist", Collections.EMPTY_LIST);
			}
			Object old = RADFunction.nullsub(file.get("fields"),
					new ArrayList<Object>());
			Object man = RADFunction.nullsub(file.get("mandatory"),
					new ArrayList<Object>());
			Object def = RADFunction.nullsub(file.get("defaults"),
					new ArrayList<Object>());
			Object avail = RADFunction.nullsub(file.get("avail"),
					new ArrayList<Object>());
			Object valid = RADFunction.nullsub(file.get("validations"),
					new ArrayList<Object>());
			Object vis = RADFunction.nullsub(file.get("invisible"),
					new ArrayList<Object>());
			Object ro = RADFunction.nullsub(file.get("readonly"),
					new ArrayList<Object>());
			Object cap = file.get("captions");
			Object mfields = RADFunction.nullsub(file.get("matchfields"),
					new ArrayList<Object>());
			Object ldap = RADFunction.nullsub(file.get("ldapattrname"),
					new ArrayList<Object>());
			Object encrypt = RADFunction.nullsub(file.get("encrypt"),
					new ArrayList<Object>());
			Object ext = RADFunction.nullsub(file.get("extattrname"),
					new ArrayList<Object>());
			Object systemFields = RADFunction.nullsub(
					file.get("system_fields"), new ArrayList<Object>());
			Object globallist = RADFunction.nullsub(file.get("globallist"),
					new ArrayList<Object>());
			Object sysFieldType = RADFunction.nullsub(file.get("sysFieldType"),
					new ArrayList<Object>());
			Object fieldUsage = RADFunction.nullsub(file.get("fieldUsage"),
					new ArrayList<Object>());
			file.put("mandatory", new ArrayList<Object>());
			file.put("ldapattrname", new ArrayList<Object>());
			file.put("fields", new ArrayList<Object>());
			file.put("types", new ArrayList<Object>());
			file.put("avail", new ArrayList<Object>());
			file.put("invisible", new ArrayList<Object>());
			file.put("readonly", new ArrayList<Object>());
			file.put("captions", new ArrayList<Object>());
			file.put("defaults", new ArrayList<Object>());
			file.put("matchfields", new ArrayList<Object>());
			file.put("validations", new ArrayList<Object>());
			file.put("encrypt", new ArrayList<Object>());
			file.put("extattrname", new ArrayList<Object>());
			file.put("system_fields", new ArrayList<Object>());
			file.put("name", name);
			file.put("fieldUsage", new ArrayList<Object>());
			file.put("sysFieldType", new ArrayList<Object>());
			file.put("globallist", new ArrayList<Object>());
			file.put("sqlbasename",
					RADFunction.nullsub(file.get("sqlbasename"), sqlBaseName));
			file.put("system", "miscellaneous");
			file.put("author", RADFunction.nullsub(file.get("author"),
					UpgradePhase.UPGRADE_USER));
			file.put("create_date",
					RADFunction.nullsub(file.get("create_date"), new Date()));
			file.put("editor", UpgradePhase.UPGRADE_USER);
			file.put("last_edit_date", new Date());
			file.put("unique_key", new ArrayList<Object>());

			for (Key key : dbdict.getKey()) {
				if (key.getFlags() == KeyType.IR_KEY.getFlags()) {
					file.put("type", "IR");
					file.put("condition",
							RADFunction.nullsub(file.get("condition"), true));
				} else if (key.getFlags() == KeyType.UNIQUE.getFlags()) {
					file.put("unique_key", key.getName());
				}
			}
			String tmpDatadictQry = (String) RADFunction.nullsub(
					"name~=" + file.get("name") + " and sqlbasename="
							+ file.get("sqlbasename"), false);

			// macro.build.field.array
			String typeName = "datadict";
			Double[] type = new Double[] { 1d, 2d, 3d, 4d, 8d };
			boolean sort = true;
			boolean root = false;
			List<Object> types = new ArrayList<Object>();

			// TODO
		}

		logger.info("  Finished updating Data Policy records.");
	}

	@Override
	public void upgrade() {
		// TODO Auto-generated method stub
		
	}
}
